package com.neuedu.controller;

import com.alibaba.fastjson2.JSONObject;
import com.neuedu.base.R;
import com.neuedu.model.ChongWu;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.ChongWuService;
import com.neuedu.service.impl.ChongWuServiceImpl;
import com.neuedu.vo.PageVO;
import org.apache.tomcat.util.codec.binary.Base64;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;



public class ChongWuController {

    private ChongWuService chongWuService=new ChongWuServiceImpl();

    /**
     * 分页查询
     * @param jsonObject
     * @return
     */
    public R page(JSONObject jsonObject){
        String minDate = jsonObject.getString("minDate");
        String maxDate = jsonObject.getString("maxDate");
        String likeName = jsonObject.getString("likeName");
        Integer pageNo = jsonObject.getInteger("pageNo");
        Integer pageSize = jsonObject.getInteger("pageSize");
        PageVO<ChongWu> pageVo = chongWuService.selectPage(minDate, maxDate, likeName, pageNo, pageSize);
        return R.ok("分页数据查询成功").setData(pageVo);
    }

    public static final String FILE_PATH="E:\\java07";

    /**
     * 添加
     * @param jsonObject
     * @return
     */
    public R add(JSONObject jsonObject){
        Integer communityid = jsonObject.getInteger("communityid");
        String suoshuchengyuan = jsonObject.getString("suoshuchengyuan");
        String chongwumingcheng = jsonObject.getString("chongwumingcheng");
        String chongwuyanse = jsonObject.getString("chongwuyanse");
        String shouyangshijian = jsonObject.getString("shouyangshijian");
        String beizhu = jsonObject.getString("beizhu");
        String base64=jsonObject.getString("base64");

        byte[] bytes = Base64.decodeBase64(base64);
        String fileName= UUID.randomUUID().toString().replace("-","").toUpperCase()+".jpg";
        File file=new File(FILE_PATH+ File.separator+fileName);
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream(file);
            fos.write(bytes);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String chongwutupian=fileName;

        ChongWu chongWu=new ChongWu();
        chongWu.setCommunityid(communityid);
        chongWu.setSuoshuchengyuan(suoshuchengyuan);
        chongWu.setChongwumingcheng(chongwumingcheng);
        chongWu.setChongwuyanse(chongwuyanse);
        chongWu.setBeizhu(beizhu);
        chongWu.setShouyangshijian(shouyangshijian);
        chongWu.setChongwutupian(chongwutupian);

        int result = chongWuService.addChongWu(chongWu);
        if(result>0){
            return R.ok("新增成功");
        }else{
            return R.error("新增失败");
        }
    }

    /**
     * 删除
     * @param map
     * @return
     */
    public R del(Map<String,String[]> map){
        String [] idsStr = map.get("id");
        Integer [] ids=new Integer[idsStr.length];
        for (int i = 0; i < ids.length; i++) {
            ids[i]=Integer.parseInt(idsStr[i]);
        }
        //调用service层，把ids传入，service帮我删去数据。
        int result = chongWuService.delChongWu(ids);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    public R query(Map<String,String[]> map){
        Integer id = Integer.parseInt(map.get("id")[0]);

        ChongWu chongWu = chongWuService.queryChongWu(id);

        return R.ok("详细信息查询成功").setData(chongWu);
    }

    /**
     * 修改
     * @param jsonObject
     * @return
     */
    public R save(JSONObject jsonObject){

        Integer id = jsonObject.getInteger("id");
        Integer communityid = jsonObject.getInteger("communityid");
        String suoshuchengyuan = jsonObject.getString("suoshuchengyuan");
        String chongwumingcheng = jsonObject.getString("chongwumingcheng");
        String chongwuyanse = jsonObject.getString("chongwuyanse");
        String shouyangshijian = jsonObject.getString("shouyangshijian");
        String beizhu = jsonObject.getString("beizhu");
        String base64=jsonObject.getString("base64");
        ChongWu chongWu = new ChongWu();

        if(base64!=null){
            byte[] bytes = Base64.decodeBase64(base64);
            String fileName=UUID.randomUUID().toString().replace("-","").toUpperCase()+".jpg";
            File file=new File(FILE_PATH+ File.separator+fileName);
            FileOutputStream fos=null;
            try {
                fos=new FileOutputStream(file);
                fos.write(bytes);
                fos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(fos!=null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String chongwutupian=fileName;
            chongWu.setChongwutupian(chongwutupian);
        }else{
            chongWu.setChongwutupian(null);
        }
        chongWu.setId(id);
        chongWu.setCommunityid(communityid);
        chongWu.setSuoshuchengyuan(suoshuchengyuan);
        chongWu.setChongwumingcheng(chongwumingcheng);
        chongWu.setChongwuyanse(chongwuyanse);
        chongWu.setBeizhu(beizhu);
        chongWu.setShouyangshijian(shouyangshijian);

        //service方法的保存
        int result = chongWuService.saveChongWu(chongWu);
        if(result>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }

    }
}
